package cn.jlkj2012.csmall.product.teacher.ex.handler;

import cn.jlkj2012.csmall.product.teacher.ex.ServiceException;
import cn.jlkj2012.csmall.product.teacher.web.JsonResult;
import cn.jlkj2012.csmall.product.teacher.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e) {
        log.debug("开始处理BindException");
        return JsonResult.fail(e);
    }


    @ExceptionHandler
    public String handleNullPointerException(NullPointerException e) {
        return "程序出现了空指针异常，已经被处理！";
    }

    @ExceptionHandler
    public String handleRuntimeException(RuntimeException e) {
        return "程序出现了RuntimeException，已经被处理！";
    }

    @ExceptionHandler
    public JsonResult handleBindException(BindException e) {
        String message = "请求参数格式错误！";
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, message);
    }
}
